package com.ruoyi.clap.controller;

import com.ruoyi.clap.domain.XlkjClap;
import com.ruoyi.clap.service.IXlkjClapService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.DictStrUtils;
import com.ruoyi.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 随手拍Controller
 *
 * @author wang
 * @date 2022-01-16
 */
@RestController
@RequestMapping("/h5/clap/clap")
public class XlkjClapControllerH5 extends BaseController
{
    @Autowired
    private IXlkjClapService xlkjClapService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询随手拍列表
     */
//    @PreAuthorize("@ss.hasPermi('clap:clap:list')")
    @GetMapping("/list")
    public TableDataInfo list(XlkjClap xlkjClap)
    {
        if(xlkjClap.getPhone() == null){
            //筛选（已通过的）
            xlkjClap.setStatus("2");
        }
        startPage();
        List<XlkjClap> list = xlkjClapService.selectXlkjClapList(xlkjClap);
        for(XlkjClap clap:list){

            //多图片路径处理
            String[] split = clap.getImg().split(",");
            String imgStr = "";
            for(int i = 0;i<split.length;i++){
                imgStr += UrlUtils.getUrl() + split[i];
                if(i != split.length-1){
                    imgStr += ",";
                }
            }
            clap.setImg(imgStr);
            //用户查询
            clap.setUserName(sysUserService.selectUserById(clap.getUserId()).getUserName());
        }
        return getDataTable(list);
    }

    /**
     * 导出随手拍列表
     */
    @PreAuthorize("@ss.hasPermi('clap:clap:export')")
    @Log(title = "随手拍", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjClap xlkjClap)
    {
        List<XlkjClap> list = xlkjClapService.selectXlkjClapList(xlkjClap);
        ExcelUtil<XlkjClap> util = new ExcelUtil<XlkjClap>(XlkjClap.class);
        return util.exportExcel(list, "随手拍数据");
    }

    /**
     * 获取随手拍详细信息
     */
//    @PreAuthorize("@ss.hasPermi('clap:clap:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        XlkjClap clap = xlkjClapService.selectXlkjClapById(id);
        //字典值翻译
        clap.setType(DictUtils.getDictLabel(DictStrUtils.CLAP_TYPE,clap.getType()));
        //多图片路径处理
        String[] split = clap.getImg().split(",");
        String imgStr = "";
        for(int i = 0;i<split.length;i++){
            imgStr += UrlUtils.getUrl() + split[i];
            if(i != split.length-1){
                imgStr += ",";
            }
        }
        clap.setImg(imgStr);
        return AjaxResult.success(clap);
    }

    /**
     * 新增随手拍
     */
//    @PreAuthorize("@ss.hasPermi('clap:clap:add')")
//    @Log(title = "随手拍", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XlkjClap xlkjClap)
    {
        SysUser sysUser = sysUserService.selectUserByUserName(xlkjClap.getPhone());
        xlkjClap.setUserId(sysUser.getUserId());
        //状态
        xlkjClap.setStatus("1");
        return toAjax(xlkjClapService.insertXlkjClap(xlkjClap));
    }

    /**
     * 修改随手拍
     */
    @PreAuthorize("@ss.hasPermi('clap:clap:edit')")
    @Log(title = "随手拍", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjClap xlkjClap)
    {
        return toAjax(xlkjClapService.updateXlkjClap(xlkjClap));
    }

    /**
     * 删除随手拍
     */
    @PreAuthorize("@ss.hasPermi('clap:clap:remove')")
    @Log(title = "随手拍", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjClapService.deleteXlkjClapByIds(ids));
    }
}
